/*
 * Copyright (c) 2009, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * /kiddie-cpp/sys/alphabet/ivec.c
 *
 *  Created on: Dec 25, 2009
 *      Author: artur
 */
#include <kiddie/kiddie.h>
#include <sys/types.h>

#include <Console.h>
#include <hw/Platform.h>

extern kernel::Console cout;
extern kernel::Platform hwplatform;

/**
 * This is an interface with atoms.S handlers
 */
#ifdef __cplusplus
extern "C"
{
#endif
reg_t
__up_trap_handler(struct cpu_regs *regs)
{
    ::cout << "[traps] BUG!!!!!!!!!!!!! Trap handler called\n";
    //    ::cout << (int) regs->trap_no << "\n";
    ::hwplatform.eventController.onTrap(regs);
}

reg_t
__up_interrupt_handler(struct cpu_regs *regs)
{
    ::hwplatform.eventController.onInterrupt(regs);
}

reg_t
__up_syscall_handler(struct cpu_regs *regs)
{
    reg_t res;
#ifdef IRQDEBUG
    ::cout << "[ __up_syscall_handler ] Syscall #" << (int)regs->trap_no << "\n";
#endif
    res = ::hwplatform.eventController.onSyscall(regs);
    return res;
}

reg_t
__up_undefined_handler(struct cpu_regs *regs)
{
    ::cout << "[ ivec ] undefined instr\n";
}

reg_t
__up_prefetch_handler(struct cpu_regs *regs)
{
    ::cout << "[ ivec ] prefetch abort\n";
}

reg_t
__up_abort_handler(struct cpu_regs *regs)
{
    ::cout << "[ ivec ] data abort\n";
    *::hwplatform.scheduler << kernel::cmd::STOP;
    // dump registers
    ::cout << kernel::Console::HEX;
    ::cout << "r0=" << regs->r0 << " r1=" << regs->r1 << "\n";
    ::cout << "r2=" << regs->r2 << " r3=" << regs->r3 << "\n";
    ::cout << "r4=" << regs->r4 << " r5=" << regs->r5 << "\n";
    ::cout << "r6=" << regs->r6 << " r7=" << regs->r7 << "\n";
    ::cout << "r8=" << regs->r8 << " r9=" << regs->r9 << "\n";
    ::cout << "r10=" << regs->r10 << " r11=" << regs->r11 << "\n";
    ::cout << "r12=" << regs->r12 << " sp=" << regs->sp << "\n";
    ::cout << "lr=" << regs->lr << " pc=" << regs->pc << "\n";
    ::cout << "svc_sp=" << regs->svc_sp << " svc_lr=" << regs->svc_lr << "\n";
    ::cout << "cpsr=" << regs->cpsr << "\n";
}
#ifdef __cplusplus
}
;
#endif

namespace syscalls
{
/*
 __inline__ vaddr_t remap(paddr_t _a, ssize_t _s)
 {
 volatile register long __res;
 __asm__ volatile (
 "int $0x40"
 : "=a" (__res)
 : "a" ((long)(2)),"b" ((long)(_a)), "c" ((long)(_s))
 );

 return (vaddr_t) __res;
 }
 */
}
